package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.PopularityAdapter;
import cst.neu.yuejian_31.bean.ClubBean;
import cst.neu.yuejian_31.iface.ClubSearchListener;
import cst.neu.yuejian_31.model.ClubSearchModel;

public class ClubSearchActivity extends BaseActivity implements View.OnClickListener,ClubSearchListener {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private PopularityAdapter popularityAdapter;
    private List<ClubBean> list=null;
    private View view=null;
    private Context context;
    private ImageView searchClub,btn_return;
    private EditText clubName;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_club_search);
        initView();
        initEvent();
        initData();
    }

    private void initData() {
        context=this;
        recyclerView= (RecyclerView)findViewById(R.id.recyclerView_searchclub);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        popularityAdapter=new PopularityAdapter(context);
        popularityAdapter.setList(list);
        recyclerView.setAdapter(popularityAdapter);
// 设置Item之间间隔样式
        recyclerView .addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
    }

    private void initEvent() {
        searchClub.setOnClickListener(this);
        btn_return.setOnClickListener(this);
    }


    private void initView() {
        searchClub= (ImageView) findViewById(R.id.imageView_searchclub_button);
        clubName= (EditText) findViewById(R.id.editText_search_club);
        btn_return= (ImageView) findViewById(R.id.imageView_searchclub_return);

    }

    @Override
    public void onSuccess(List<ClubBean> clubBeen) {
        list=clubBeen;
        popularityAdapter.setList(list);



    }

    @Override
    public void onFail(String msg) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_searchclub_button:
                search();
                break;
            case R.id.imageView_searchclub_return:
                finish();
                break;
        }

    }

    private void search() {
        String name=clubName.getText().toString();
        ClubSearchModel clubSearchModel=new ClubSearchModel();
        clubSearchModel.searchClub(name,this);

    }
}
